const app = getApp();
Page({
  data: {
    vtabs: [],
    activeTab: 0,
    page: {},
    productList: []
  },

  onLoad() {
    this.getCategoryList();
    //this.findProduct("零食");
  },
  onShow() {
    this.getCategoryList();
  },
/**
 * 点击类目切换内容
 * @param {*} e 
 */
  chooseCategory(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      activeTab: index
    });
    const category = this.data.vtabs[index];
    this.findProduct(category.name);
  },
  /**
   * 初始化获取类目
   */
  getCategoryList() {
    const that = this;
    wx.cloud.callFunction({
      // 需调用的云函数名
      name: 'categoryApi',
      // 传给云函数的参数
      data: {
        method: "queryList",
        args: {}
      },
      // 成功回调
      complete: (res)=>{
        that.setData({
          vtabs: res.result.rstData.data
        });
        this.findProduct(this.data.vtabs[0].name);
      }
    })
  },
  /**
   * 根据类目查询商品
   * @param {*} category 
   */
  findProduct(category) {
    console.log("查询类目：", category);
    const that = this;
    const param = {
      pageSize: 20,
      pageNo: 1,
      category: category
    };
    wx.cloud.callFunction({
      // 需调用的云函数名
      name: 'productapi',
      // 传给云函数的参数
      data: {
        method: "findPageByCategory",
        args: param
      },
      // 成功回调
      complete: (res)=>{
        console.log(res, "产品列表")
        that.setData({
          page: res.result.rstData,
          productList: res.result.rstData.list
        });
      }
    });
  },
  /**
   * 添加购物车
   */
  addBuyCar(e) {
    console.log(e, "添加购物车");
    const that = this;
    let param = e.currentTarget.dataset.product;
    app.addBuyCar(param);
  },
  // 去商品详细页
  goDetail(e) {
    wx.navigateTo({
      url: '../shop/detail?id=' + e.currentTarget.dataset.id,
    })
  }
})
